from tkinter import *
from settings import *
from tkinter import ttk
from tkinter import messagebox

# 初始化主窗口
app = Tk()
app.title("4aTools  v1.0.5")
app.geometry('1000x600+100+200')
app.iconbitmap("4atools.ico")

# 初始化操作类型下拉框
type_combobox = ttk.Combobox(app)
type_combobox.grid(row=0, column=0, sticky="w", padx=10, pady=5)
type_combobox['value'] = list(TYPE_DICT)
type_combobox.current(0)

# 初始化操作下拉框
operation_combobox = ttk.Combobox(app, width=70)
operation_combobox.grid(row=0, column=1, sticky="w", padx=10, pady=5)
operation_combobox['value'] = list(TYPE_DICT[type_combobox.get()])
operation_combobox.current(0)

# 初始化描述:
op_info_text = StringVar()
op_info = Label(app, textvariable=op_info_text, width=55, height=4)
op_info.grid(row=1, column=0, sticky="W", padx=10, pady=1)
op_info_text.set(TYPE_DICT[type_combobox.get()][operation_combobox.get()][1])

op_format_text = StringVar()
op_format = Label(app, textvariable=op_format_text, width=70, height=4)
op_format.grid(row=1, column=1, sticky="W", padx=10, pady=1)
op_format_text.set(TYPE_DICT[type_combobox.get()][operation_combobox.get()][2])

# 初始化文本框
parameter_text = Text(app, width=55, height=35, undo=True, autoseparators=False, padx=5, pady=5)
parameter_text.grid(row=2, column=0, sticky="e", padx=10, pady=5)
result_text = Text(app, width=70, height=35, undo=True, autoseparators=False, padx=5, pady=5)
result_text.grid(row=2, column=1, sticky="e", padx=10, pady=5)


# 操作下拉框绑定类型下拉框
def operation_combobox_bind(event):
    operation_combobox['value'] = list(TYPE_DICT[type_combobox.get()])
    operation_combobox.current(0)
    op_info_text.set(TYPE_DICT[type_combobox.get()][operation_combobox.get()][1])
    op_format_text.set(TYPE_DICT[type_combobox.get()][operation_combobox.get()][2])


type_combobox.bind("<<ComboboxSelected>>", operation_combobox_bind)


def operation_info_bind(event):
    op_info_text.set(TYPE_DICT[type_combobox.get()][operation_combobox.get()][1])
    op_format_text.set(TYPE_DICT[type_combobox.get()][operation_combobox.get()][2])
    generating(event)


operation_combobox.bind("<<ComboboxSelected>>", operation_info_bind)


def generating(event):
    result_text.delete(1.0, "end")
    result_text.insert(
        END,
        TYPE_DICT[type_combobox.get()][operation_combobox.get()][0](parameter_text.get(1.0, "end"))
        # text.get(1.0, "end")
    )


parameter_text.bind("<KeyPress>", generating)
parameter_text.bind("<KeyRelease>", generating)
parameter_text.bind("<Button>", generating)
parameter_text.bind("<ButtonRelease>", generating)


def copy_result():
    result = result_text.get(1.0, "end")
    app.clipboard_clear()  # Optional.
    app.clipboard_append(result.rstrip())
    app.update()


copy_button = Button(app, text='复制\n语句', command=copy_result).grid(row=2, column=2, sticky="e", padx=5, pady=5)

# 主循环
app.mainloop()
